package com.hhh.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hhh.Mapper.DishFlavorMapper;
import com.hhh.Pojo.DishFlavor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class DishFlavorService implements com.hhh.Service.DishFlavorService {

    @Autowired
    DishFlavorMapper dishFlavorMapper;

    //    添加菜品口味
    @Override
    public Integer Insert(List<DishFlavor> dishFlavorList) {
        int num = 0;
        for (DishFlavor dishFlavor : dishFlavorList) {
            num += dishFlavorMapper.insert(dishFlavor);
        }
        return num;
    }

    //    根据菜品id查询菜品口味
    @Override
    public List<DishFlavor> SelectByDishID(long id) {
        QueryWrapper<DishFlavor> wrapper = new QueryWrapper<>();
        wrapper.eq("dish_id", id);
        return dishFlavorMapper.selectList(wrapper);
    }

    //    修改菜品口味
    @Override
    public Integer Update(List<DishFlavor> arr) {
        log.info("修改菜品口味");
        QueryWrapper<DishFlavor> wrapper = new QueryWrapper<>();

        int len = arr.size();
        int num = 0;
        while (num < len && arr.get(num) != null) {
            wrapper.eq("dish_id", arr.get(num).getDishId());
            dishFlavorMapper.update(arr.get(num), wrapper);
            num++;
        }
        return num == len ? 1 : 0;
    }

    //    删除菜品口味
    @Override
    public Integer DeleteByDishId(long DishId) {
        QueryWrapper<DishFlavor> wrapper = new QueryWrapper<>();
        wrapper.eq("dish_id", DishId);
        return dishFlavorMapper.delete(wrapper);
    }


}
